package com.gexiaobao.pigeon.app.tools.tongliansdk

import android.app.Activity
import android.text.TextUtils
import com.allinpay.unifypay.sdk.Allinpay
import com.gexiaobao.pigeon.R
import com.gexiaobao.pigeon.app.util.EncryptUtil
import java.nio.charset.StandardCharsets

object TonglianSdk {

    var appid = "00184486"
    var orgid = ""
    var cusid = "552452048160FH2"
    var key = "CJWJum6I"


    /**
     * 签名订单信息
     *
     * @param activity 跳出的activity
     * @param amount 金额
     * @param validtime 超时时间
     * @param notifyurl 通知后台的url
     * @param reqsn 订单号

     * @return
     */
    fun openPay(activity: Activity, amount: String, validtime: String, notifyurl: String, reqsn: String) {
        //该params应由服务端返回，demo只为方便提供示例
        val params = java.util.HashMap<String, String>()
        params["limitpay"] = "no_credit"
        params["trxamt"] = amount
        params["reqsn"] = reqsn
        params["subject"] = activity.getString(R.string.android_order)
        params["trxreserve"] = activity.getString(R.string.remark)
        params["validtime"] = validtime
        params["schemeurl"] = "allinpaysdk://www.gexiaobao.cn/result"
        params["notifyurl"] = notifyurl
        params["appid"] = appid
        params["orgid"] = orgid
        params["cusid"] = cusid
        params["signtype"] = "RSA"
        params["sign"] = signParam(params)
        Allinpay.openPay(activity, params)
    }

    /**
     * 签名订单信息
     *
     * @param params
     * @return
     */
    private fun signParam(params: HashMap<String, String>): String {
        try {
            val signtype = params["signtype"]
            val md5Sign: String
            if (!TextUtils.isEmpty(signtype) && "RSA" == signtype) {
                val blankData: String = EncryptUtil.getSignStr(params, null, true)
                md5Sign = EncryptUtil.rsa(blankData, SYB_RSACUSPRIKEY)
            } else {


                params["key"] = key
                val blankData: String = EncryptUtil.getSignStr(params, null, true)
                md5Sign = EncryptUtil.md5(blankData.toByteArray(StandardCharsets.UTF_8))
                params.remove("key")
            }
            return md5Sign
        } catch (e: Exception) {
            e.printStackTrace()
        }
        return ""
    }

    const val SYB_RSACUSPRIKEY =
        "MIIEvwIBADANBgkqhkiG9w0BAQEFAASCBKkwggSlAgEAAoIBAQDICwcVVye0RvoTrxPiupdp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